<!--
Copyright (C) 2011 Opera Software ASA.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY OPERA SOFTWARE ASA. ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 -->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="../../resources/js-test-style.css"/>
        <script src="../../resources/js-test-pre.js"></script>
        <script src="../resources/webgl-test.js"></script>
        <script id="vshader" type="x-shader/x-vertex">
            attribute vec2 pos;

            void main()
            {
                gl_Position = vec4(pos, 0, 1);
            }
        </script>

        <script id="fshader" type="x-shader/x-fragment">
            precision mediump float;

            void main()
            {
                gl_FragColor = vec4(0, 1, 0, 1);
            }
        </script>

        <script>
            // Check a single 32-bit RGBA pixel.
            function checkPixel(buf, index, correct) {
                for (var i = 0; i < 4; ++i) {
                    if (buf[index + i] != correct[i]) {
                        return false;
                    }
                }
                return true;
            }

            // Check the line loop by reading the pixels and making sure just the edge
            // pixels are green and the rest are black.
            function checkLineLoop(gl, w) {
                var buf = new Uint8Array(w * w * 4);
                gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf);
                var green = [0,255,0,255];
                var black = [0,0,0,255];
                var isCorrect = true;
                for (var j = 0; j < w * w * 4; j += 4) {
                    var correct = black;
                    if (j < w * 4 || j > w * (w - 1) * 4 || j % (w * 4) == 0 || j % (w * 4) == (w - 1) * 4) {
                        correct = green;
                    }
                    // ignore corner pixels
                    if ((j == 0) || (j == 4*(w-1)) || (j == 4*w*(w-1)) || (j== 4*(w*w - 1))) {
                        continue;
                    }
                    if (!checkPixel(buf, j, correct)) {
                        isCorrect = false;
                        break;
                    }
                }
                if (isCorrect) {
                    testPassed("Line loop was drawn correctly.");
                } else {
                    testFailed("Line loop was drawn incorrectly.");
                }
            }

            // Check the tri fan by reading the pixels and making sure they are all green.
            function checkTriFan(gl, w) {
                buf = new Uint8Array(w * w * 4);
                gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf);
                var filled = true;
                for (var j = 0; j < w * w * 4; j += 4) {
                    if (!checkPixel(buf, j, [0,255,0,255])) {
                        filled = false;
                        break;
                    }
                }
                if (filled) {
                    testPassed("Triangle fan was drawn correctly.");
                } else {
                    testFailed("Triangle fan was drawn incorrectly.");
                }
            }

            function runTest()
            {
                var gl = initWebGL('testbed', 'vshader', 'fshader', ['pos'], [0, 0, 0, 1], 1, { antialias: false });
                if (!gl) {
                    testFailed('initWebGL(..) failed');
                    return;
                }
                var w = document.getElementById('testbed').width;

                gl.enableVertexAttribArray(0);

                //---------- LINE_LOOP----------
                var d = 1/w;
                var vertices = new Float32Array([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
                var vertBuf = gl.createBuffer();
                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
                var indBuf = gl.createBuffer();
                var indices = new Uint16Array([0, 1, 2, 3]);
                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

                debug('Draw a square using a line loop and verify that it draws all four sides and nothing else.');
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawArrays(gl.LINE_LOOP, 0, vertices.length / 2);
                checkLineLoop(gl, w);

                debug('Draw a square using an indexed line loop and verify that it draws all four sides and nothing else.');
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_SHORT, 0);
                checkLineLoop(gl, w);

                vertices = new Float32Array([0, 0, 0, 0, 0, 0, -1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
                vertBuf = gl.createBuffer();
                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
                indBuf = gl.createBuffer();
                indices = new Uint16Array([0, 1, 2, 3, 4, 5, 6]);
                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

                debug('Draw a square using a line loop with a vertex buffer offset and verify that it draws all four sides and nothing else.');
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawArrays(gl.LINE_LOOP, 3, vertices.length / 2 - 3);
                checkLineLoop(gl, w);

                debug('Draw a square using an indexed line loop with an index buffer offset and verify that it draws all four sides and nothing else.');
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawElements(gl.LINE_LOOP, indices.length - 3, gl.UNSIGNED_SHORT, 3 * 2);
                checkLineLoop(gl, w);

                //---------- LINE_LOOP UBYTE ----------
                var degenVerts = new Array(252 * 2);
                for (var j = 0; j < 252 * 2; ++j) {
                    degenVerts[j] = -1+d;
                }
                degenVerts = degenVerts.concat([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
                vertices = new Float32Array(degenVerts);
                vertBuf = gl.createBuffer();
                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
                indBuf = gl.createBuffer();
                var degenInd = new Array(252);
                for (var j = 0; j < 252; ++j) {
                    degenInd[j] = j;
                }
                degenInd = degenInd.concat([252, 253, 254, 255]);
                indices = new Uint8Array(degenInd);
                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

                debug('Draw a square using an ubyte indexed line loop with 256 indices and verify that it draws all four sides and nothing else.');
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_BYTE, 0);
                checkLineLoop(gl, w);


                //---------- TRIANGLE_FAN ----------
                vertices = new Float32Array([0, 0, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1]);
                vertBuf = gl.createBuffer();
                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
                indices = new Uint16Array([0,1,2,3,4,5]);
                indBuf = gl.createBuffer();
                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

                debug('Draw a filled square using a triangle fan and verify that it fills the entire canvas.');
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawArrays(gl.TRIANGLE_FAN, 0, vertices.length / 2);
                checkTriFan(gl, w);

                debug('Draw a filled square using an indexed triangle fan and verify that it fills the entire canvas.');
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawElements(gl.TRIANGLE_FAN, indices.length, gl.UNSIGNED_SHORT, 0);
                checkTriFan(gl, w);

                vertices = new Float32Array([1, 1, 1, 1, 1, 1, 0, 0, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1]);
                vertBuf = gl.createBuffer();
                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
                indices = new Uint16Array([0,1,2,3,4,5,6,7,8]);
                indBuf = gl.createBuffer();
                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

                debug('Draw a filled square using a triangle fan with a vertex buffer offset and verify that it fills the entire canvas.');
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawArrays(gl.TRIANGLE_FAN, 3, vertices.length / 2 - 3);
                checkTriFan(gl, w);

                debug('Draw a filled square using an indexed triangle fan with an index buffer offset and verify that it fills the entire canvas.');
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawElements(gl.TRIANGLE_FAN, indices.length - 3, gl.UNSIGNED_SHORT, 3 * 2);
                checkTriFan(gl, w);
            }
        </script>
    </head>
    <body>
        <canvas id="testbed" width="10px" height="10px" style="width:50px; height:50px"></canvas>
        <div id="description"></div>
        <div id="console"></div>
        <script>
            description('Verify that LINE_LOOP and TRIANGLE_FAN works correctly.');
            runTest();
            successfullyParsed = true;
        </script>
        <script src="../../resources/js-test-post.js"></script>
    </body>
</html>
